<?php
include_once("../includes/global.php");
$script_tmp = explode('/', $_SERVER['SCRIPT_NAME']);
$sctiptName = array_pop($script_tmp);
include_once("auth.php");
include_once("../includes/page_utf_class.php");
//=================
if(!empty($_POST['submit']) && $_POST['submit']!="")
{
	if(is_array($_POST["de"]))
	{
		$id=implode(",",$_POST["de"]);
		$sql="delete from ".EXTENDFILE." where id in ($id)";
		$db->query($sql);
		echo "<script>alert('".lang_show('edit_s')."');</script>";
	}	
}
$module_name=$_GET['module'];
if($_GET['creat']==1)
{
	$table_name=$config['table_pre'].$module_name."_defind";
	//-----------先行删除表
		$db->query("SHOW TABLES LIKE '".$table_name."'");
		if($db->num_rows()==1)
		{
			$db->query("SHOW COLUMNS FROM ".$table_name);
			foreach ($columns=$db->getRows() as $item){
				$fields[]=$item['Field'];
			}
			
			
			$sql="select * from ".EXTENDFILE." where module='$_GET[module]'";
			$db->query($sql);
			$sre=$db->getRows();
			foreach ($sre as $item){
				if (in_array($item['catName'],$fields)){
						
				}else{
					$db->query("ALTER TABLE ".$table_name." ADD COLUMN `".$item['catName']."`  $item[fieldPro]($item[fieldLen]) default '$item[defaultValue]' COMMENT '$item[catInfo]'");
				}
			}
		
		}else{
		//-----------按类生成扩展表
		$csql="
		CREATE TABLE `".$table_name."` (
		  `id` int(11) NOT NULL auto_increment,
		  `pid` varchar(15) default NULL,";
		  	//--------------
     $sql="select * from ".EXTENDFILE." where module='$_GET[module]'";
     $db->query($sql);
     $sre=$db->getRows();
			foreach($sre as $sv)
			{
				 $csql.="\n`$sv[catName]` $sv[fieldPro]($sv[fieldLen]) default '$sv[defaultValue]' COMMENT '$sv[catInfo]',";
			}
			//--------------
		$csql.="
		  PRIMARY KEY  (`id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
		";
    $db->query($csql);
		}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<TITLE><?php echo lang_show('admin_system');?></TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<link href="main.css" rel="stylesheet" type="text/css" />
<script language="javascript">
function do_select()
{
	 var box_l = document.getElementsByName("de[]").length;
	 for(var j = 0 ; j < box_l ; j++)
	 {
	  	if(document.getElementsByName("de[]")[j].checked==true)
	  	  document.getElementsByName("de[]")[j].checked = false;
		else
		  document.getElementsByName("de[]")[j].checked = true;
	 }
}
function CheckIsSelect(){
	var event = document.getElementsByName("de[]");
	for(var j=0; j<event.length; j++){
        if(event[j].checked){// && event[j].disabled
			return true;
        }
	}
	return false;
}
</script>
</head>
<body>
<div class="bigbox">
	<div class="bigboxhead">
    <span class="cbox <?php if(empty($_GET['cat_type']) || $_GET['cat_type']=='afield') echo 'on';?>"><a href="add_field.php?cat_type=afield&module=<?php echo $_GET['module'];?>"><?php echo lang_show('fcat');?></a></span>
    <span class="cbox <?php if(!empty($_GET['cat_type'])&&$_GET['cat_type']=='efield') echo 'on';?>"><a href="edit_field.php?cat_type=efield&module=<?php echo $_GET['module'];?>"><?php echo lang_show('efcat');?></a></span>
    </div>
	<div class="bigboxbody">
<form action="" method="get">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
		<input onclick="window.location='edit_field.php?creat=1&module=<?php echo $_GET['module'];?>';" class="btn" value="生成字段" name="" type="button" />
	</td>
  </tr>
</table>
</form>
<form method="post" action="">
  <table width="100%" border="0" align="left" cellpadding="2" cellspacing="0" >
    <tr class="theader"> 
      <td width="35"><input onClick="do_select()" style="width:20px;" name="" type="checkbox" class="checkbox"></td>
      <td width="*%"><?php echo lang_show('field_edit_display_name');?></td>
      <td width="127"><?php echo lang_show('field_edit_pro_');?></td>
      <td width="129"><?php echo lang_show('field_edit_product_name');?></td>
      <td width="111" align="center"><?php echo lang_show('field_edit_info_name');?></td>
      <td width="224" align="center"><?php echo lang_show('field_edit_display_type');?></td>
	  <td width="85" align="center"><?php echo lang_show('field_edit_para');?></td>
    </tr>
    <?php
		$sql=NULL;
		$tsql="select id,catName,catDes,catInfo,defaultValue,fieldPro,displayType
			 from ".EXTENDFILE." where module='$_GET[module]' $sql order by id desc";
		//=============================
	  	$page = new Page;
		$page->listRows=20;
		if (!$page->__get('totalRows')){
			$db->query($tsql);
			$page->totalRows = $db->num_rows();
		}
        $tsql .= "  limit ".$page->firstRow.",20";
		$pages = $page->prompt();
		//==============================
		$db->query($tsql);
		$re=$db->getRows();
		foreach($re as $va)
		{
	?> 
    <tr> 
      <td width="25"> 
      <input name="de[]" type="checkbox" class="checkbox" style="width:20px;" value="<?php echo $va['id']; ?>">		</td>
      <td width="*%">
	  <?php echo $va['catName']; ?>(<?php echo $va['catInfo']; ?>)</td>
      <td width="127"><?php echo $va['fieldPro'];?></td>
      <td>
      <?php
      	if($va['defaultValue']!=""){
			echo $va['defaultValue'];
		}else{
			echo "无默认值";
		}
	  ?></td>
      <td align="center"><?php echo $va['catDes'];?></td>
      <td width="224" align="center"> 
      <?php
	  $editTempType=lang_show('displayType');
      	echo $editTempType[$va['displayType']];
	  ?> </td>
	  <td  width="85" align="center"><a href="add_field.php?id=<?php echo $va['id']; ?>&cat_type=afield&module=<?php echo $_GET['module'];?>"><?php echo lang_show('edit');?></a></td>    
    </tr>
    <?php 
	}
	?> 
	    <tr >
      <td colspan="4"><input class="btn" type="submit" name="submit" value="<?php echo lang_show('delete')?>" onClick="if(CheckIsSelect()){return confirm('<?php echo lang_show('edit_yes')?>');}else{alert('<?php echo lang_show('edit_no')?>');return false;}"></td>
      <td colspan="3" align="center"><div class="page"><?php echo $pages?></div></td>
      </tr>
  </table>
</form>
</div>
</div>
</div>
</body>
</HTML>